package thread;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Zhou_Bing on 2018/11/30.
 */
public class Demo {

    public static void main(String[] args){

        final CountDownLatch downLatch = new CountDownLatch(4);


        /*final List list = new LinkedList();
        List list1 = new ArrayList(10000000);
        List list2 = new ArrayList(10000000);
        List list3 = new ArrayList(10000000);
        List list4 = new ArrayList(10000000);

        long start = System.currentTimeMillis();
        for(int i=0;i<4;i++){
            final int num = i;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    int count = 0;
                    List list = null;
                    switch (num){
                        case 0:
                            list = list1;
                        case 1:
                            list = list2;
                        case 2:
                            list = list3;
                        case 3:
                            list = list4;
                    }
                    for (int i = num; i < 10000000; i = i + 4) {
                        list.add(i);
                    }
                    downLatch.countDown();
                }
            });
            thread.start();
        }*/

        final List list = new Vector();

        long start = System.currentTimeMillis();
        for(int i=0;i<4;i++){
            final int num = i;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    int count = 0;
                    for (int i = num; i < 10000000; i = i + 4) {
                        System.out.println("线程"+num);
                        list.add(i);
                    }
                    downLatch.countDown();
                }
            });
            thread.start();
        }
        try {
            downLatch.await();
            System.out.println(System.currentTimeMillis()-start);
            /*list.addAll(list1);
            list.addAll(list2);
            list.addAll(list3);
            list.addAll(list4);*/
            System.out.println(list.size());
            System.out.println(System.currentTimeMillis()-start);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
